home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BCCGRX12.ZIP / contrib / bcc2grx / src / bccgrx.c next >
Text File  |  1993-07-24  |  8KB  |  223 lines

  1. /*
  2.  *  BCC2GRX  -  Interfacing Borland based graphics programs to LIBGRX
  3.  *  Copyright (C) 1993  Hartmut Schirmer
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *
  20.  *  Contact :                Hartmut Schirmer
  21.  *                           Feldstrasse 118
  22.  *                    D-2300 Kiel 1
  23.  *                           Germany
  24.  *
  25.  *                    No telephone calls please !
  26.  *
  27.  *           NOTE : Please use ZIP 24105 after July 1 1993 !!
  28.  *
  29.  */
  30.  
  31. #define __BCCGRX_C
  32.  
  33. #include "bccgrx00.h"
  34. #include <malloc.h>
  35.  
  36. /* ----------------------------------------------------------------- */
  37.  
  38. int           __gr_Mode = 0;                    /* actual graphics mode     */
  39. int           __gr_INIT = FALSE;                /* TRUE after initgraph()   */
  40. char          __gr_BGICHR[128];                 /* Path to .chr files       */
  41. int           __gr_MaxMode = 0;                 /* Last available mode      */
  42. int           __gr_Result = grOk;               /* stores error code        */
  43. int           __gr_X, __gr_Y;                   /* graphics cursor pos      */
  44. int           __gr_vpl, __gr_vpt,               /* actual viewport          */
  45.           __gr_vpr, __gr_vpb;
  46. int           __gr_color;                       /* drawing color            */
  47. int           __gr_colorbg;                     /* background color         */
  48. int           __gr_colorfill;                   /* fill color               */
  49. int           __gr_WR = GrWRITE;                /* Write mode               */
  50. int           __gr_lstyle = SOLID_LINE;         /* Actual line style        */
  51. int           __gr_fpatno = SOLID_FILL;         /* Actual filling pattern   */
  52. int           __gr_Xasp = 10000;                /* Aspect ratio             */
  53. int           __gr_Yasp = 10000;
  54. int           __gr_clip = TRUE;                 /* actual clipping state    */
  55. int           __gr_ADAPTER = GR_VGA;            /* Adapter used             */
  56.  
  57. short         *__gr_modeindx = NULL;            /* mode # -> driver mode    */
  58. GR_DRIVER_MODE_ENTRY *__gr_DATA;                /* ptr to driver mode data  */
  59. GrPattern     __gr_fillpattern;                 /* GRX filling settings     */
  60. GrLineOption  __gr_Line;                        /* GRX line settings        */
  61.  
  62. char __gr_fpatterns[][8] = {                    /* BGI fill patterns        */
  63.   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},   /* EMPTY_FILL        */
  64.   { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},   /* SOLID_FILL        */
  65.   { 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00},   /* LINE_FILL         */
  66.   { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80},   /* LTSLASH_FILL      */
  67.   { 0xE0, 0xC1, 0x83, 0x07, 0x0E, 0x1C, 0x38, 0x70},   /* SLASH_FILL        */
  68.   { 0xF0, 0x78, 0x3C, 0x1E, 0x0F, 0x87, 0xC3, 0xE1},   /* BKSLASH_FILL      */
  69.   { 0xA5, 0xD2, 0x69, 0xB4, 0x5A, 0x2D, 0x96, 0x4B},   /* LTBKSLASH_FILL    */
  70.   { 0xFF, 0x88, 0x88, 0x88, 0xFF, 0x88, 0x88, 0x88},   /* HATCH_FILL        */
  71.   { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81},   /* XHATCH_FILL       */
  72.   { 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33},   /* INTERLEAVE_FILL   */
  73.   { 0x80, 0x00, 0x08, 0x00, 0x80, 0x00, 0x08, 0x00},   /* WIDE_DOT_FILL     */
  74.   { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00},   /* CLOSE_DOT_FILL    */
  75.   { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}    /* USER_FILL         */
  76. };
  77.  
  78. int __gr_BGI_w = 640; /* resulution and colors needed to emulate */
  79. int __gr_BGI_h = 480; /* BGI driver modes                        */
  80. int __gr_BGI_c = 16;  /* default : Standard VGA                  */
  81.  
  82. /* ----------------------------------------------------------------- */
  83.  
  84. void __gr_set_up_modes(void)
  85. {
  86.   GR_DRIVER_MODE_ENTRY *tm, *gm;
  87.   int mode;
  88.   static int DidInit = FALSE;
  89.  
  90.   if (DidInit) return;
  91.  
  92.   MM = 1;
  93.   GrSetMode( GrCurrentMode());                       /* Init grx */
  94.   __gr_ADAPTER = GrAdapterType();
  95.   if (__gr_ADAPTER == GR_S3)
  96.     __gr_ADAPTER = GR_VGA;
  97.   GrGetDriverModes(&tm, &__gr_DATA);
  98.   if (__gr_DATA == NULL) {       /* Will happen with .GRD drivers */
  99.     __gr_modeindx = NULL;
  100.     DidInit = TRUE;
  101.     return;
  102.   }
  103.   gm = __gr_DATA;
  104.   mode = __FIRST_DRIVER_SPECIFIC_MODE-1;
  105.   while (gm->width != 0) {
  106.     if (gm->BIOS_mode != 0xFF)
  107.       ++mode;
  108.     gm += 1;
  109.   }
  110.   __gr_modeindx = (short *) malloc(sizeof(short) * mode);
  111.   if (__gr_modeindx == NULL) {
  112.     DidInit = TRUE;   /* Handle no mem like .GRD driver */
  113.     return;           /* should never (??) occur ...    */
  114.   }
  115.   __gr_modeindx[0] = -1;  /* GR_default_graphics               */
  116.   __gr_modeindx[1] = -2;  /* GR_biggest_noninterlaced_graphics */
  117.   __gr_modeindx[2] = -3;  /* GR_biggest_graphics               */
  118.   __gr_modeindx[3] = -4;  /* BGI emulation                     */
  119.   gm = __gr_DATA;
  120.   mode = __FIRST_DRIVER_SPECIFIC_MODE-1;
  121.   while (gm->width != 0) {
  122.     if (gm->BIOS_mode != 0xFF)
  123.       __gr_modeindx[++mode] = gm-__gr_DATA;
  124.     gm += 1;
  125.   }
  126.   MM = mode;
  127.   DidInit = TRUE;
  128. }
  129.  
  130. /* ----------------------------------------------------------------- */
  131. void graphdefaults(void)
  132. {
  133.   ERR          = grOk;
  134.   moveto( 0, 0);
  135.   COL = FILL   = GrWhite();
  136.   COLBG        = GrBlack();
  137.   __gr_WR      = GrWRITE;
  138.  
  139.   _DO_INIT_CHECK;
  140.   GrSetContext( NULL);         /* ViewPort == Full screen */
  141.   VL = VT = 0;
  142.   VR = getmaxx();
  143.   VB = getmaxy();
  144.   __gr_clip = TRUE;
  145.  
  146.   __gr_lstyle     = SOLID_LINE;
  147.   LNE.lno_pattlen = 0;
  148.   LNE.lno_dashpat = NULL;
  149.   LNE.lno_width   = 1;
  150.  
  151.   FPATT = SOLID_FILL;
  152.   FILLP.gp_ispixmap    = 0;    /* Bitmap fill pattern */
  153.   FILLP.gp_bmp_data    = (unsigned char *)&__gr_fpatterns[FPATT];
  154.   FILLP.gp_bmp_height  = 8;
  155.   FILLP.gp_bmp_fgcolor = COL;
  156.   FILLP.gp_bmp_bgcolor = COLBG;
  157. }
  158.  
  159. /* ----------------------------------------------------------------- */
  160. void detectgraph(int *graphdriver,int *graphmode)
  161. {
  162.   __gr_set_up_modes();
  163.   *graphdriver = __gr_ADAPTER;  /* set by _gr_set_up_modes () */
  164.   *graphmode     = 0;           /* Default graphics mode      */
  165. }
  166.  
  167. /* ----------------------------------------------------------------- */
  168. void setgraphmode(int mode)
  169. {
  170.   GR_DRIVER_MODE_ENTRY *gm;
  171.  
  172.   _DO_INIT_CHECK;
  173.   switch (mode) {
  174.     case GRX_DEFAULT_GRAPHICS:
  175.       GrSetMode( GR_default_graphics);
  176.       break;
  177.     case GRX_BIGGEST_NONINTERLACED_GRAPHICS:
  178.       GrSetMode( GR_biggest_noninterlaced_graphics);
  179.       break;
  180.     case GRX_BIGGEST_GRAPHICS:
  181.       GrSetMode( GR_biggest_graphics);
  182.       break;
  183.     case GRX_BGI_EMULATION:
  184.       GrSetMode( GR_width_height_color_graphics,
  185.           __gr_BGI_w, __gr_BGI_h, __gr_BGI_c);
  186.       break;
  187.     default:
  188.       if (mode < __FIRST_DRIVER_SPECIFIC_MODE || mode > MM) {
  189.     ERR = grInvalidMode;
  190.     return;
  191.       }
  192.       gm = __gr_DATA+__gr_modeindx[mode];
  193.       GrSetMode( GR_width_height_color_graphics,
  194.          gm->width, gm->height, gm->number_of_colors);
  195.       break;
  196.   }
  197.   __gr_Mode = mode;
  198.   graphdefaults();
  199.   GrClearScreen(BLACK);
  200. }
  201.  
  202. /* ----------------------------------------------------------------- */
  203. void initgraph(int *graphdriver, int *graphmode, char *pathtodriver)
  204. {
  205.   ERR = grOk;
  206.   if (!__gr_INIT) {
  207.     __gr_set_up_modes();
  208.     if (ERR != grOk) return;
  209.     if ( *graphdriver != NATIVE_GRX || *graphmode < 0 || *graphmode > MM)
  210.     *graphmode = 0;
  211.     __gr_INIT = TRUE;
  212.     setgraphmode(*graphmode);
  213.     if (ERR != grOk) {
  214.       __gr_INIT = FALSE;
  215.       return;
  216.     }
  217.   }
  218.   if (*graphmode == 0) *graphdriver = __gr_ADAPTER;
  219.           else *graphdriver = NATIVE_GRX;
  220.   strcpy( __gr_BGICHR, pathtodriver);
  221. }
  222. 
  223.